package cn.ymjr.sys.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@SuppressWarnings("unchecked")
@Component
public class RedisUtil {
    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean setNx(String key,String value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    /**
     * 自增
     */
    public long incr(String key, long delta){
        if(delta<0){
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }
    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }
    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0){
            redisTemplate.delete(keys);
        }
    }
    /**
     * 批量获取keys
     */
    public Set<String> getPattern(String pattern){
        Set<String> keys = stringRedisTemplate.keys(pattern);
        return keys;
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }
    public Long getTtl(String key){
        return redisTemplate.getExpire(key);
    }
    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * sorted set
     */
    public boolean zadd(String key,Object val){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        Boolean add = zSetOperations.add(key, val,DateUtil.getDefaultDate());
        return add;
    }

    /**
     *sorted set delete
     * @return
     */
    public boolean zdelete(String key,Object val){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        Long remove = zSetOperations.remove(key, val);
        if(remove==0){
            return false;
        }else{
            return true;
        }
    }
    /**
     * sorted set list
     */
    public Set zlist(String key){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        Set range = zSetOperations.range(key, 0, -1);
        return range;
    }
    public void hset(final String key,Map<String,Object> map){
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.putAll(key,map);
    }
    public Map hget(final String key){
        HashOperations hashOperations = redisTemplate.opsForHash();
        Map map = hashOperations.entries(key);
        return map;
    }
    public Object hgetHk(String key,Object hashKey){
        HashOperations hashOperations = redisTemplate.opsForHash();
        return hashOperations.get(key, hashKey);
    }
    public void hput(String key,Object hashKey,Object val){
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key,hashKey,val);
    }
    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
